package com.ltsznh.android.locationreporting;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;


public class LoginActivity extends Activity {

    private EditText etUserCode;
    private EditText etPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        ActionBar actionBar = this.getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        etUserCode = (EditText) findViewById(R.id.etLoginUserCode);
        etPwd = (EditText) findViewById(R.id.etLoginPwd);

//        InputMethodManager imm =
//                (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//
//        imm.showSoftInputFromInputMethod(etUserCode.getWindowToken(), 0);

//      etUserCode.setFocusable(true);

//        etpwd.

    }

    public void login(View view) {
        if(etUserCode.getText().toString().trim().equals("")){
         alter(getResources().getString(R.string.login_null_userCode));
            return;
        }
        if(etPwd.getText().toString().equals("")){
            alter(getResources().getString(R.string.login_null_pwd));
            return;
        }
        if (view.getId() == (R.id.btnLogin)) {
            Toast.makeText(this, "login", Toast.LENGTH_SHORT).show();

            AccountManager accountManager = AccountManager.get(LoginActivity.this);
            Account[] accounts = accountManager.getAccounts();
            for (int i = 0; i < accounts.length; i++) {
                Log.v("dddd", accounts[i].type + " " + accounts[i].name + " " + accounts[i].toString());
            }


            Bundle addAccountOptions = null;

//            accountManager.addAccount("type","authTokenType",new String[]{},addAccountOptions,addAccountActivity,accountManagerCallback,handler);
        }
    }

    private void alter(String message) {
        Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                Log.d(this.getClass().toString(), "click nothing");
                return super.onOptionsItemSelected(item);
        }
    }
}
